<?php

namespace App\Enums\Workflow;

use App\Models\OpenShop\Common\OpenShopOrderManagerModel;

class WorkflowEnum
{
    // 审批流类型
    const WORKFLOW_TYPE_OPEN_SHOP_RENEW = 1;
    const WORKFLOW_TYPE_SUPPLIER_TENDER = 2;
    const WORKFLOW_TYPE_TEXT            = [
        self::WORKFLOW_TYPE_OPEN_SHOP_RENEW => '开店续签',
        self::WORKFLOW_TYPE_SUPPLIER_TENDER => '供应链投标',
    ];

    // 节点类型
    const NODE_TYPE_SEQUENCE = 1;
    const NODE_TYPE_AND      = 2;
    const NODE_TYPE_OR       = 3;
    const NODE_TYPE_IF       = 4;

    const NODE_TYPE_TEXT = [
        self::NODE_TYPE_SEQUENCE => '顺序',
        self::NODE_TYPE_AND      => '会签',
        self::NODE_TYPE_OR       => '或签',
        self::NODE_TYPE_IF       => '条件',
    ];

    // 复杂节点
    const NODE_COMPLEX_TYPE = [
        self::NODE_TYPE_AND,
        self::NODE_TYPE_OR,
        self::NODE_TYPE_IF,
    ];

    // 业务类型
    const BUSINESS_TYPE_SUBMIT_APPLY  = 1;
    const BUSINESS_TYPE_FIX_AUDIT     = 2;
    const BUSINESS_TYPE_PREPARE_STAGE = 3;
    const BUSINESS_TYPE_YES           = 4;
    const BUSINESS_TYPE_NO            = 5;
    const BUSINESS_TYPE_RENEW_SIGN    = 6;

    const BUSINESS_TYPE_TEXT = [
        self::BUSINESS_TYPE_SUBMIT_APPLY  => '提交申请',
        self::BUSINESS_TYPE_FIX_AUDIT     => '用户审批',
//        self::BUSINESS_TYPE_PREPARE_STAGE => '是否进入筹备阶段',
//        self::BUSINESS_TYPE_YES           => '是',
//        self::BUSINESS_TYPE_NO            => '否',
        self::BUSINESS_TYPE_RENEW_SIGN    => '续约签署',
    ];

    // 处理人类型
    const HANDLER_TYPE_HEAD      = 1;
    const HANDLER_TYPE_SUPERVISE = 2;
    const HANDLER_TYPE_PROVINCE  = 3;
    const HANDLER_TYPE_MERCHANT  = 4;
    const HANDLER_TYPE_SHOP_MANAGER = 5;

    const HANDLER_TYPE_TEXT = [
        0                            => '无',
        self::HANDLER_TYPE_HEAD      => '总部',
        self::HANDLER_TYPE_SUPERVISE => '督导',
        self::HANDLER_TYPE_PROVINCE  => '省总',
        self::HANDLER_TYPE_MERCHANT  => '加盟商本人',
        self::HANDLER_TYPE_SHOP_MANAGER => '门店店长',
    ];

    // open_shop_order_manager.type => handler_type
    const OPEN_SHOP_MANAGER_TYPE_TO_HANDLER_TYPE = [
        OpenShopOrderManagerModel::MANAGER_TYPE_5 => self::HANDLER_TYPE_SUPERVISE,
    ];

    // 节点状态
    const NODE_STATUS_WAIT  = 0;
    const NODE_STATUS_ING   = 1;
    const NODE_STATUS_OK    = 2;
    const NODE_STATUS_CHILD = 3;

    const STATUS_TEXT = [
        self::NODE_STATUS_WAIT  => '未开始',
        self::NODE_STATUS_ING   => '进行中',
        self::NODE_STATUS_OK    => '已完成',
        self::NODE_STATUS_CHILD => '等待子节点审批',
    ];

    // 条件公式formula
    const FORMULA_EQUALS   = '=';
    const FORMULA_GT       = '>';
    const FORMULA_GT_EQ    = '>=';
    const FORMULA_LT       = '<';
    const FORMULA_LT_EQ    = '<=';
    const FORMULA_UNEQUALS = '!=';
    const FORMULA_IN       = 'in';
    const FORMULA_NOT_IN   = 'not in';

    const FORMULA_TEXT = [
        self::FORMULA_EQUALS   => '等于',
        self::FORMULA_GT       => '大于',
        self::FORMULA_GT_EQ    => '大于等于',
        self::FORMULA_LT       => '小于',
        self::FORMULA_LT_EQ    => '小于等于',
        self::FORMULA_UNEQUALS => '不等于',
        self::FORMULA_IN       => '包含',
        self::FORMULA_NOT_IN   => '不包含',
    ];

    public static function formatEnums(string $field, array $enums): array
    {
        return [
            'field' => $field,
            'enums' => collect($enums)->map(function ($v, $k) {
                return [
                    'text'  => $v,
                    'value' => $k,
                ];
            })->values()->toArray(),
        ];
    }

    /**
     * 获取所有enums
     * @return array
     */
    public static function getEnums(): array
    {
        return [
            self::formatEnums('nodeType', self::NODE_TYPE_TEXT),
            self::formatEnums('businessType', self::BUSINESS_TYPE_TEXT),
            self::formatEnums('handlerType', self::HANDLER_TYPE_TEXT),
            self::formatEnums('formula', self::FORMULA_TEXT),
        ];
    }
}
